var BannerTable = {
    id: "bannerTable",//表格id
    seItem: null,		//选中的条目
    me : ""
};


/**
 * 初始化整个表格
 */
BannerTable.init = function(){
    var defaultColunms = this.initColumn();
    var bstable = new BSTable(BannerTable.id,Feng.ctxPath+"/banner/allParam",defaultColunms);
    bstable.setData({});
    this.me = bstable.init();
};

/**
 * 初始化表格的列
 */
BannerTable.initColumn = function(){
    var columns = [
        {title: '序号',align: 'center',valign: 'middle',
            formatter: function indexFormatter(value, row, index) {//在表格前加上序号
                return index+1;
            }},
        {title: '轮播图片',field: 'imgName',align: 'center',valign: 'middle',
            formatter: function(value,row,index){
                var imgUrl = Feng.ctxPath + "/img/logo_b.png";
                if (value) {
                    imgUrl = Feng.ctxPath + "/file/getImage/" + value;
                }
                return '<img style="width: 150px;height: 80px;" onclick="showPic(\'' + value + '\');" src="' + imgUrl + '" class="img-rounded" >';
            }},
        {title: '链接地址',field: 'url',align: 'center',valign: 'middle'},
        {title: '创建时间',field: 'createDate',align: 'center',valign: 'middle'},
        {title: '最后更新时间',field: 'updateDate',align: 'center',valign: 'middle'},
        {title: '状态',field: 'status',align: 'center',valign: 'middle',
            formatter:function (value,row,index) {
                if(value == "0"){
                    return "已禁用";
                }else if(value == "1"){
                    return "已启用";
                }
            }},
        {title: '操作',field: '#',align: 'center',valign: 'middle',
            formatter:function(value,row,index){
                //var d = '<button class="btn btn-primary btn-xs" onclick="viewItems(\''+ row.id + '\')" title="查看使用详情"><i class="glyphicon glyphicon-align-justify"></i></button> &nbsp;&nbsp;';
                var e = '<button class="btn btn-primary btn-xs" onclick="editBtn(\''+ row.id + '\')" title="编辑"><i class="glyphicon glyphicon-pencil"></i></button> &nbsp;&nbsp;';
                // var f = '<button class="btn btn-danger btn-xs" onclick="deleteBtn(\''+ row.id+'\',\''+ row.couponCardNo+'\')" title="删除"><i class="glyphicon glyphicon-trash"></i></button>';
                return e;
            }
        }];
    return columns;
};

/**
 * 点击搜索按钮
 * @returns
 */
function clickSearch(){
    var queryData = {};
    var val_search_couponCardNo = $("#txt_search_couponCardNo").val();

    if (val_search_couponCardNo != "" || val_search_couponCardNo != null){
        queryData['couponCardNo'] =  val_search_couponCardNo;
    }

    BannerTable.me.destroy(queryData);
    // BannerTable.me.refresh({query: queryData});
}

/**
 * 点击添加按钮时，跳出添加用户对话框
 * @returns
 */
function clickAddBtn(){
    var index = layer.open({
        type: 1,
        title: '添加banner',
        skin: 'layui-layer-lan', //加上边框
        area: [BannerInfoDlg.width,BannerInfoDlg.height ],//宽高
        content: BannerInfoDlg.cache,
        success: function(layero, index){
            //层弹出成功后回调方法
            BannerInfoDlg.renderAdd();

        },
        maxmin: true,
        scrollbar: false,
        btn:['保存','关闭'],
        btnAlign: 'r',  //默认值
        yes:function (index,layero) {
            BannerInfoDlg.addSubmit();
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },btn2:function (index) {
            layer.close(index);
        },
        cancel:function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    BannerInfoDlg.currentLayerIndex = index;
}

/**
 * 点击编辑按钮时，跳出banner详情对话框
 * @returns
 */
function editBtn(uid){
    var index = layer.open({
        type: 1,
        title: '编辑Banner',
        skin: 'layui-layer-lan',
        area: [BannerInfoDlg.width,BannerInfoDlg.height],
        content: BannerInfoDlg.cache,
        success: function(layero, index){
            //层弹出成功后回调方法
            BannerInfoDlg.renderAdd();
            BannerInfoDlg.load(uid);
        },
        maxmin:true,
        scrollbar: false,
        btn:['保存','关闭'],
        btnAlign: 'r',  //默认值
        yes:function (index,layero) {
            BannerInfoDlg.editSubmit(index);
            //layer.close(index); //如果设定了yes回调，需进行手工关闭
        },btn2:function (index) {
            layer.close(index);
        },
        cancel:function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    BannerInfoDlg.currentLayerIndex = index;
}


/**
 * 删除某个banner
 * @param uid 用户的id
 * @returns
 */
function deleteBtn(uid,deleteName){
    layer.confirm('是否删除Banner:'+deleteName, {icon: 10, title:'是否删除Banner?'}, function(index){
        var ajax = new $ax(Feng.ctxPath + "/banner/deleteById", function(data){
            if (data.success){
                Feng.success("删除成功!");
                BannerTable.me.refresh();
                layer.close(index);
            }else if(data.code === 2){
                Feng.fail(data.msg);
            }else{
                Feng.fail("删除失败!");
            }
        },function(data){
            Feng.error("删除失败!");
        });
        ajax.data={id:uid};
        ajax.start();
    });
}

/**
 * 查看banner使用详情
 * @returns
 */
function viewItems(dataId){
    var index = layer.open({
        type: 1,
        title: 'banner使用详情列表',
        skin: 'layui-layer-lan', //加上边框
        area: [BannerTable.width, BannerTable.height],//宽高
        content: BannerTable.cache,
        success: function(layero, index){
            //初始化优惠券管理表格
            BannerTable.init(dataId);
        },
        maxmin: true,
        scrollbar: false,
        cancel:function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    BannerTable.currentLayerIndex = index;
}

/**
 * 点击看大图
 * @param imgName
 */
function showPic(imgName) {
    layer.photos({
        photos: {
            "title": "banner图片", //相册标题
            "id": 123, //相册id
            "start": 0, //初始显示的图片序号，默认0
            "data": [   //相册包含的图片，数组格式
                {
                    "alt": "banner图片",
                    "pid": 666, //图片id
                    "src": Feng.ctxPath + "/file/getImage/" + imgName, //原图地址
                    "thumb": Feng.ctxPath + "/file/getImage/" + imgName //缩略图地址
                }
            ]
        },
        anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
    });
}


$(function(){
    //初始化优惠券管理表格
    BannerTable.init();

    BannerInfoDlg.init();
});
